<?php
class PdoClass{
    protected $pro;
    public function __construct(){
        $this->pro= new PDO("mysql:host=localhost;dbname=miss",'root','root');
    }
    /*
     * 添加
     * */
    public function add($name,$sex,$age){
        $sql = "insert into people set name='{$name}',sex='{$sex}',age = '{$age}'";
        $this->pro->exec($sql);
        return true;
    }
    /*
     * 展示
     * */
    public function show($page){
        $sql="select * from people";
        $res=$this->pro->query($sql);
        $count=$res->rowCount();
        $size=3;
        $pagenum=ceil($count/$size);
        //$page=isset($_GET['page'])?$_GET['page']:'1';
        $limit=($page-1)*$size;
        $sql1="select * from people limit $limit,$size";
        $res1=$this->pro->query($sql1);
        $data['pageinfo']=$res1->fetchAll();
        $pagestr['prev']=$page-1==1?1:$page-1;
        $pagestr['next']=$page+1>=$pagenum?$pagenum:$page+1;
        $pagestr['pagenum']=$pagenum;
        $data['pagestr']=$pagestr;
        return $data;
    }

    /*
     * 删除
     * */
    public function delete($id){
        $sql = "delete from people where id=:id";
        $res = $this->pro->prepare($sql);
        $res->execute([':id'=>$id]);
        return true;
    }
    /*
     * 修改
     * */
    public function update($id,$name,$sex,$age){
        $sql = "update people set name='{$name}',sex='{$sex}',age='{$age}' where id=:id";
        $res = $this->pro->prepare($sql);
        $res->execute([':id'=>$id]);
        return true;
    }
    /*
     * 查一条数据
     * */
    public function find($id){
        $sql = "select * from people where id=:id";
        $res = $this->pro->prepare($sql);
        $res->execute([':id'=>$id]);
        return $res->fetch();

    }
    /*
     * 生成静态化
     * */
    public function state($id,$name,$sex,$age){
        ob_start();
        include 'aa.html';
        $text = ob_get_clean();
        file_put_contents('./html/'.$id.'.html',$text);
        return true;
    }
    /*
     * 搜索
     * */
    public function sear($cha){
        $sql = "select * from people where name like '%$cha%'";
        $res = $this->pro->query($sql);
        $size=3;
        $count=$res->rowCount();
        $pagenum=ceil($count/$size);
        $page=isset($_GET['page'])?$_GET['page']:'1';
        $limit=($page-1)*$size;
        $sql1="select * from people where name like '%$cha%' limit $limit,$size";
        $res1=$this->pro->query($sql1);
        $data['pageinfo']=$res1->fetchAll();
        $pagestr['prev']=$page-1==1?1:$page-1;
        $pagestr['next']=$page+1>=$pagenum?$pagenum:$page+1;
        $pagestr['pagenum']=$pagenum;
        $data['pagestr']=$pagestr;
        return $data;
    }
}





?>